<template>
    <div>
      <van-form @submit="onSubmit">
        <van-field
          v-model="username"
          name="username"
          label="用户名"
          placeholder="用户名"
          :rules="[
            { required: true, message: '用户名不能为空' },
            {
              pattern: /^[a-z0-9]{4,10}$/i,
              message: '用户名只能数字字母组成长度4到10位',
              trigger: 'onChange',
            },
          ]"
        />
        <van-field
          v-model="password"
          type="password"
          name="password"
          label="密码"
          placeholder="密码"
          :rules="[
            { required: true, message: '密码不能为空' },
            {
              pattern: /^[a-z0-9]{6,10}$/i,
              message: '密码只能数字字母组成长度6到10位',
              trigger: 'onChange',
            },
          ]"
        />
      
        <div style="margin: 16px">
          <van-button round block type="info" native-type="submit"
            >提交</van-button
          >
        </div>
      </van-form>
    </div>
  </template>
  
  <script>
  import { userQuery } from "@/model/userModel.js";
  
  
  export default {
    data() {
      return {
        username: "", //用户名
        password: "", //密码
      };
    },
    methods: {

      async onSubmit(values) {
        //检查用户名是否存在
        let res2 = await userQuery({
          username: values.username,
          password:values.password
        });
        if(res2.data.length){
            let info  = res2.data[0];
            window.localStorage.setItem("userinfo",JSON.stringify(info));
            let  backUrl = window.localStorage.getItem("backUrl");
            if(backUrl){
                this.$router.push(backUrl);
            }else{
                this.$router.push("/");
            }
           
        }else{
            this.$toast.fail("用户名和密码不匹配");
        }
       
      },
    },
  };
  </script>
  
  <style></style>
  